package com.link.base.base.approval.service;

import com.link.base.base.approval.dao.mybatis.mapper.FlowRecordMapper;
import com.link.base.base.approval.model.FlowRecord;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 14:45
 */

@Service
public class FlowRecordServiceImpl extends BasicServiceImpl<FlowRecord> implements FlowRecordService {

    @Resource
    private FlowRecordMapper flowRecordMapper;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Override
    public BasicMapper<FlowRecord> getBasicMapper() throws Exception {
        return flowRecordMapper;
    }

    /**
     * 分页查询业务对象审批历史
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<FlowRecord> queryObjFlowRecordListPage(FlowRecord record) throws BasicServiceException, Exception {
        return flowRecordMapper.queryObjFlowRecordListPage(record);
    }


    /**
     * 新建流程记录
     *
     * @param record
     * @throws BasicServiceException
     */
    @Override
    public void flowRecordInsert(FlowRecord record) throws BasicServiceException, Exception {
        flowRecordMapper.insert(record);
    }

    /**
     * 新增审批历史记录信息
     *
     * @param flowId
     * @param approvalNodeId
     * @param operateDesc
     * @param comments
     * @throws Exception
     */
    @Override
    public void flowRecordInsert(Long flowId, Long approvalNodeId, String operateDesc, String comments) throws Exception {
        FlowRecord record = new FlowRecord();
        record.setId(keyGenerateDao.keyGenerate());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        record.setApprovalEnd(dateFormat.format(new Date()));
        record.setApprovalOpera(operateDesc);
        record.setApprovalView(comments);
        record.setApprovalUserId(UserUtil.getUser().getId());
        record.setFlowId(flowId);
        record.setApprovalNodeId(approvalNodeId);
        flowRecordMapper.insert(record);
    }

    /**
     * 查询当前审批节点，本次审核历史记录
     *
     * @param flowId
     * @param approvalNodeId
     * @return List
     * @throws Exception
     */
    @Override
    public List<FlowRecord> queryCurentNodeRecord(Long flowId, Long approvalNodeId) throws Exception {
        FlowRecord record = new FlowRecord();
        record.setFlowId(flowId);
        record.setApprovalNodeId(approvalNodeId);
        String lastTime = flowRecordMapper.queryLastNodeMaxTime(record);
        FlowRecord entity = new FlowRecord();
        entity.setAttr1(String.valueOf(flowId));
        entity.setApprovalNodeId(approvalNodeId);
        entity.setCreated(lastTime);
        return flowRecordMapper.queryCurrRecordSelect(entity);
    }

    /**
     * 根据审批流程id获取最新审批到哪条记录
     *
     * @author 姚江漪
     * @date 2019/07/01 10:07
     */
    @Override
    public FlowRecord queryLastNodeMaxSn(FlowRecord flowRecord) throws Exception {
        return flowRecordMapper.queryLastNodeMaxSn(flowRecord);
    }

}
